package com.hn658.groupshare.frontend.action.group.impl;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hn658.framework.common.AbstractBaseService;
import com.hn658.framework.common.IServiceResponse;
import com.hn658.framework.dataaccess.pagination.GenericPage;
import com.hn658.framework.shared.exception.BusinessException;
import com.hn658.groupshare.dictionary.service.IDictionaryService;
import com.hn658.groupshare.frontend.action.account.IUserAccountAction;
import com.hn658.groupshare.frontend.action.group.vo.GroupShareRequest;
import com.hn658.groupshare.frontend.action.group.vo.GroupShareResponse;
import com.hn658.groupshare.group.entity.GroupInfoEO;
import com.hn658.groupshare.group.exception.GroupInfoException;
import com.hn658.groupshare.group.service.IGroupInfoService;

/**
 * 群管理接入
 * 
 * @ClassName: GroupShareActionImpl
 * @author ztjie
 * @date 2015年1月26日 上午11:46:00
 */
@Controller("groupShareAction")
@Path("group")
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
public class GroupShareActionImpl extends AbstractBaseService implements
		IUserAccountAction {

	@Autowired
	private IGroupInfoService groupInfoService;
	
	@Autowired
	private IDictionaryService dictionaryService;

	@Path("queryGroupInfoList")
	@POST
	public IServiceResponse queryGroupInfoList(GroupShareRequest request) {
		GroupShareResponse response = new GroupShareResponse();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			if (!StringUtils.isEmpty(request.getGroupNameLike())) {
				params.put("groupNameLike", request.getGroupNameLike());
			}
			if (request.getGroupType()!=null) {
				params.put("groupType", request.getGroupType());
			}
			String sortString = "id";
			if (request.getSortType()!=null) {
				sortString = "create_time";
				params.put("ORDERBY", sortString);
			}
			
			params.put("isDeleted", false);
			
			GenericPage<GroupInfoEO> gp = groupInfoService.queryGroupsByParams(
					params, request.getLimit(), request.getStart(),
					sortString, false);
			response.setGenericPage(gp);
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
	
	@Path("createGroupInfo")
	@POST
	public IServiceResponse createGroupInfo(GroupShareRequest request) {
		GroupShareResponse response = new GroupShareResponse();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			
			params.put("qrcodeText", request.getGroupInfo().getQrcodeText());
			params.put("isDeleted", false);
			
			GenericPage<GroupInfoEO> gp = groupInfoService.queryGroupsByParams(params, 1, 1, "create_time", true);
			if(gp.getTotalCount()>0){
				throw new GroupInfoException(GroupInfoException.QrcodeTextIsExist);
			}
			
			groupInfoService.createGroupInfo(request.getGroupInfo());
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}

	@Path("queryGroupInfoById")
	@POST
	public IServiceResponse queryGroupInfoById(GroupShareRequest request) {
		GroupShareResponse response = new GroupShareResponse();
		try {
			GroupInfoEO groupInfo = groupInfoService.findGroupShareById(request.getId());
			response.setGroupInfo(groupInfo);
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
	
	@Path("queryTypes")
	@POST
	public IServiceResponse queryTypes() {
		GroupShareResponse response = new GroupShareResponse();
		try {
			Map<String, String> groupType = dictionaryService.selectDataByType("groupType");
			Map<String, String> sortType = dictionaryService.selectDataByType("sortType");
			response.setGroupTypeList(groupType.get("groupType"));
			response.setSortTypeList(sortType.get("sortType"));
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
}
